const curl = require('./curl');
const cheerio = require('cheerio');
const xlsx = require('./xlsx');
const moment = require('moment');

const get_world_data = function () {
  const url = "https://voice.baidu.com/act/newpneumonia/newpneumonia";

  return new Promise(function (resolve, reject) {
    curl.download(url, function (res) {
      if (res) {

        let window = {};
        let $ = cheerio.load(res);

        let V = {};
        let var_flag = "var V = V || {};";
        $('body>script:not([src])').each(function (index, ele) {

          if (ele.attribs.id == 'captain-config') {
            let ele_data = ele.children[0].data;
            V.data = JSON.parse(ele_data);
          }
        });

        const baidu_data = V.data.component[0];

        let case_list = baidu_data.caseList;
        let caseOutside_list = baidu_data.caseOutsideList;
        let summaryDataIn_list = baidu_data.summaryDataIn;
        let summaryDataOut_list = baidu_data.summaryDataOut;
        let provinceTrend_list = baidu_data.provinceTrendList;

        console.log('获取百度数据成功！');

        let data_array = [case_list, caseOutside_list, summaryDataIn_list, summaryDataOut_list, provinceTrend_list];
        resolve(data_array);
      } else {
        reject('获取百度数据失败！')
      }
    })
  });
}

const get_data = async function (file) {
  let world_data = await get_world_data();

  let case_list = world_data[0];
  let caseOutside_list = world_data[1];
  let summaryDataIn_obj = world_data[2];
  let summaryDataOut_obj = world_data[3];
  let provinceTrend_list = world_data[4];

  // 1-国内总体情况
  // 1.1- 省份情况
  let case_province_array = [
    ['省份', '确诊', '死亡', '治愈', '确诊变化', '死亡变化', '治愈变化', '重症变化', '时间']
  ];

  // 1.2- 地市情况
  let case_city_array = [
    ['省份', '城市', '确诊', '死亡', '治愈', '时间']
  ];

  case_list.forEach(province => {
    // let modifyTime = moment(province.relativeTime).format('YYYY-MM-DD HH:mm:ss');
    case_province_array.push([
      province.area, province.confirmed, province.died, province.crued, province.confirmedRelative,
      province.diedRelative, province.curedRelative, province.icuDisable, province.relativeTime
    ]);

    let cities = province.subList;
    cities.forEach(item => {
      case_city_array.push([
        province.area, item.city, item.confirmed, item.died, item.crued, province.relativeTime
      ]);
    });
  });

  // 2-国外情况
  let caseOutside_array = [
    ['国家', '确诊', '重症', '治愈', '死亡']
  ];

  caseOutside_list.forEach(ele => {
    caseOutside_array.push([ele.area, ele.confirmed, ele.icuDisable, ele.crued, ele.died]);
  });

  // 3-国内整体情况
  // let summaryDataIn_time = moment(summaryDataIn_obj.relativeTime).format('YYYY-MM-DD HH:mm:ss');
  let summaryDataIn_array = [
    ['确诊', summaryDataIn_obj.confirmed],
    ['死亡', summaryDataIn_obj.died],
    ['治愈', summaryDataIn_obj.cured],
    ['疑似', summaryDataIn_obj.unconfirmed],
    ['确诊变化', summaryDataIn_obj.confirmedRelative],
    ['疑似变化', summaryDataIn_obj.unconfirmedRelative],
    ['治愈变化', summaryDataIn_obj.curedRelative],
    ['死亡变化', summaryDataIn_obj.diedRelative],
    ['icuDisable', summaryDataIn_obj.icuDisable],
    ['时间', summaryDataIn_obj.relativeTime]
  ];

  // 4-国际整体情况
  let summaryDataOut_array = [
    ['确诊', summaryDataOut_obj.confirmed],
    ['死亡', summaryDataOut_obj.died],
    ['治愈', summaryDataOut_obj.cured],
    ['icuDisable', summaryDataOut_obj.icuDisable],
    ['icu', summaryDataOut_obj.icu],
    ['clinical', summaryDataOut_obj.clinical],
    ['icuRelative', summaryDataOut_obj.icuRelative],
    ['clinicalRelative', summaryDataOut_obj.clinicalRelative],
  ]

  // 5-各省趋势
  let provinceTrend_array = [
    ['省份', '更新日期', '确诊', '治愈', '死亡', '新增确诊']
  ];

  provinceTrend_list.forEach(ele => {
    let trendList = ele.trend.list;
    let data4 = trendList.length == 4 ? trendList[3].data : null
    provinceTrend_array.push([
      ele.name, ele.trend.updateDate, trendList[0].data, trendList[1].data, trendList[2].data, data4
    ]);
  });

  let new_file = new xlsx.Xlsx(file);

  new_file.add({
    name: '国际整体情况',
    data: summaryDataOut_array
  });

  new_file.add({
    name: '国内整体情况',
    data: summaryDataIn_array
  });

  new_file.add({
    name: '省份',
    data: case_province_array
  });

  new_file.add({
    name: '地市',
    data: case_city_array
  });

  new_file.add({
    name: '各省趋势',
    data: provinceTrend_array
  });

  await new_file.writeFileSync();
}

exports.get_data = get_data;